package com.etc.controller;


import java.util.List;

import javax.annotation.Resource;

import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.PathAndSelectScenicMessage;
import com.etc.entity.Province;
import com.etc.entity.Scenic;
import com.etc.entity.ScenicMessage;
import com.etc.entity.SdetailsWithBLOBs;
import com.etc.service.ProvinceService;
import com.etc.service.ScenicService;
import com.etc.service.impl.PlayWayServiceImpl;
import com.etc.utils.PageData;

@Controller
@RequestMapping(value = "scenic")
public class ScenicController {

	@Resource
	ScenicService sService;
	@Resource
	PlayWayServiceImpl pwsi;
	@Resource
	ProvinceService pservice;
	
	//查询所有的景点
	@RequestMapping(value = "findallScenic")
	@ResponseBody

	public PageData<ScenicMessage> findAllScenic(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
			@RequestParam(value = "pid", defaultValue = "0") int pid,
			@RequestParam(value = "cid", defaultValue = "0") int cid,
			@RequestParam(value = "hid", defaultValue = "0") int hid) {
		System.out.println("page:" + page + ",pageSize:" + pageSize + ",pid:" + pid + ",cid:" + cid + ",hid:" + hid);
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 10;
		}
		if(pid==110000||pid==120000||pid==310000||pid==500000||pid==810000||pid==820000) {
			cid=0;
		}
		PageData<ScenicMessage> pds = sService.findScenicBypIdAndcId(page, pageSize, pid, cid, hid);
	//	System.out.println(pds);
		return pds;
	}
	
	//根据省市ID以及景点主题查询符合要求的景点信息
	@RequestMapping(value="findScenicBypchId")
	@ResponseBody
	public List<Scenic> findScenicByPCidAndHid(@RequestParam(value="pid",defaultValue="0") int pid,
			@RequestParam(value="cid",defaultValue="0") int cid,@RequestParam(value="hid",defaultValue="0") int hid){
		//System.out.println("根据省市ID以及景点主题查询符合要求的景点信息pid:"+pid+",cid:"+cid+",hid:"+hid);
		if(pid==110000||pid==120000||pid==310000||pid==500000||pid==810000||pid==820000) {
			cid=0;
		}
		return sService.findScenicByPidCidAndHid(pid, cid, hid);
	}
	
	//根据景点id，与选择的套餐规划路线
	@RequestMapping(value="playScenicWayBySids",method=RequestMethod.POST)
	@ResponseBody
	public List<PathAndSelectScenicMessage> playScenicWayBySids(@RequestParam(value="pid",defaultValue="0") int pid,
			@RequestParam(value="cid",defaultValue="0") int cid,@RequestParam(value="hid",defaultValue="0") int hid,
			@RequestParam(value="rsort_rule",defaultValue="2") int rsort_rule,int[] scenicid) throws JSONException{
		List<PathAndSelectScenicMessage> passm = null;
		//System.out.println("pid:"+pid+";cid:"+cid+";hid:"+hid+";rsort_rule:"+rsort_rule+";scenicid+"+scenicid.length);
		if(scenicid.length ==1) {
			passm = pwsi.playWayOneScenic(pid, cid, hid, rsort_rule, scenicid);
		}else if(scenicid.length ==2) {
			passm = pwsi.playWayTwoScenic(pid, cid, hid, rsort_rule, scenicid);
		}else {
			passm =pwsi.playWayMoreScenic(pid, cid, hid, rsort_rule, scenicid);
		}
		return passm;
	}

	//根据省市查询该地的热门景点根据满意度进行排序
	@RequestMapping(value="findcityhotscenic")
	@ResponseBody
	public List<ScenicMessage> findScenicByPCname(@RequestParam(value="pname",defaultValue="福建省")String pname){
	//	System.out.println("pname:"+pname+";cname:"+cname);
		

		String reg = "^-?[1-9]\\d*$";
		boolean flag =pname.matches(reg);
		if(flag&&!pname.equals("0")) {
			int pid = Integer.parseInt(pname);
			Province c =  pservice.findProvinceBypId(pid);
			pname = c.getProvincename();
		}else if(pname.equals("0")){
			pname = "北京市";
		}
			pname = pname.substring(0, 2);
			//System.out.println("pname:"+pname);
		
		return sService.findHotScenicByPname(pname);
	}
	
	//根据景点id查询景点的详细信息
	@RequestMapping(value="findScenicMessagebysid")
	@ResponseBody
	public SdetailsWithBLOBs findScenicMessagebysid(int sid) {
		
		return sService.findScenicMessageBySid(sid);
	}
	
	@RequestMapping(value = "findTheme", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Scenic> listTheme(@RequestParam(value="page",defaultValue="10")Integer page,@RequestParam(value="pagesize",defaultValue="20") Integer pagesize, @RequestParam(value="content",defaultValue="")String content) {
		if (content == null) {
			content = "";
		}
		PageData<Scenic> pd = sService.selectThemeByhid(page, pagesize, content);
		pd.setCode(0);
		pd.setMsg("请求成功");
		//System.out.println(pd);
		return pd;

	}

	@RequestMapping(value = "theme")
	public String theme() {
		return "admin/admin-theme";
	}

}
